package com.example.mygame.controller;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.mygame.R;
import com.example.mygame.pojo.User;
import com.example.mygame.service.SPSave;
import com.example.mygame.service.SqliteUtils;

public class Register extends AppCompatActivity {

    private EditText userName;
    private EditText password;
    private EditText nick;
    private EditText sex;
    private EditText phone;
    private EditText age;
    private Button register;

    private SqliteUtils sqliteUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        userName = findViewById(R.id.user_name);
        password = findViewById(R.id.password);
        sex = findViewById(R.id.sex);
        age = findViewById(R.id.age);
        nick = findViewById(R.id.nick);
        phone = findViewById(R.id.phone);
        register = findViewById(R.id.register);

        sqliteUtils = new SqliteUtils(this);

        // 注册用户
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                User user = new User();
                user.setName(userName.getText().toString());
                user.setPassword(password.getText().toString());
                user.setSex(sex.getText().toString());
                user.setAge(age.getText().toString());
                user.setNick(nick.getText().toString());
                user.setPhone(phone.getText().toString());

                long l = sqliteUtils.insertData(user);

                Intent intent = new Intent(Register.this, Login.class);

                Bundle bundle = new Bundle();
                bundle.putString("userName",user.getName());
                bundle.putString("password",user.getPassword());

                intent.putExtras(bundle);

                if (l != -1) {
                    Toast.makeText(getApplicationContext(), "注册成功",
                            Toast.LENGTH_SHORT).show();
                    SPSave.saveUserInfo(Register.this,user.getName(),user.getPassword());
                } else {
                    Toast.makeText(getApplicationContext(), "注册失败",
                            Toast.LENGTH_SHORT).show();
                }


                startActivity(intent);
            }
        });

    }
}